﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using ArithCollect;
using System.Diagnostics;
using CSharpAlgorithm.Algorithm;
using System.Data;

namespace UnitTestProject1 {
    [TestClass]
    public class UnitTest1 {
        [TestMethod]
        public void M34() {
            var x = 3; var a = 5;
            Console.WriteLine("x={0},a={1}", x, a);
            x = x ^ a;
            a = x ^ a;
            x = x ^ a;
            Console.WriteLine("x={0},a={1}", x, a);
        }
        [TestMethod]
        public void M33() {
            var g = new Graph<bool>();
            g.CreateSection(new[,]{
                {false,false,false,false,false},
                {false,false,false,false,false},
                {false,false,false,false,false},
                {false,false,false,false,false},

                {false,false,false,false,false},
                {false,false,false,false,false},
            });
            g.Construct("a", 0, 0, new[] { 
                new Tople<int,int>(0,1)
            });
            g.Construct("a", 1, 0, new[] { 
                new Tople<int,int>(1,1)
            });
            g.Construct("a", 2, 0, new[] { 
                new Tople<int,int>(2,1)
            });
        }
        [TestMethod]
        public void M32() {
            var s = "aaabbbc1=0945";
            var b = "ababacb109=4";
            Console.WriteLine(s.CheckMatch(b));
        }
        [TestMethod]
        public void M31() {
            Expr s = "3^2+4^2";
            var x = s.f(
                new Tople<int, double[]>(0, new[] { 1d, 2d, 3d, 4d }),
                new Tople<int, double[]>(2, new[] { 1d, 3d, 5d, 7d })
                );
            foreach (var item in x) {
                Console.WriteLine(item);
            }
        }
        [TestMethod]
        public void M30() {
            Expr s = "3^2+4^2";
            var x = s.f(2, 1d, 2d, 3d, 4d);
            foreach (var item in x) {
                Console.WriteLine(item);
            }
        }
        [TestMethod]
        public void M29() {
            var dt = new DataTable();
            var x = dt.Compute("25 + 8 * 6 * 6 - (85 - 5 + 6) / 8.0 + (55 - 27 * 27 * 27 * 27)", null);
            Console.WriteLine(x.ToString());
        }
        [TestMethod]
        public void M282() {
            Expr s = "25+8*6^2-(85-5+6)/8*sin(150)+(55-27^4)";
            Console.WriteLine(s);
            Console.WriteLine(s.ToString());
            Console.WriteLine(25d + 8d * 6d * 6d - (85d - 5d + 6d) / 8.0d * Math.Sin(150) + (55d - 27d * 27d * 27d * 27d));
        }
        [TestMethod]
        public void M281() {
            Expr s = "25+8*6^2-(85-5+6)/8*sin(150)+(55-27^4)";
            s.RegistryFunc("sin", ps => Math.Sin(ps[0]));
            s.ToExpr();
            Console.WriteLine(s);
            Console.WriteLine(s.ToString());
            Console.WriteLine(25d + 8d * 6d * 6d - (85d - 5d + 6d) / 8.0d * Math.Sin(150) + (55d - 27d * 27d * 27d * 27d));
        }
        [TestMethod]
        public void M28() {
            Expr s = "25+8*6^2-(85-5+6)/8+(55-27^4)";
            Console.WriteLine(s);
            Console.WriteLine(s.ToString());
            Console.WriteLine(25d + 8d * 6d * 6d - (85d - 5d + 6d) / 8.0d + (55d - 27d * 27d * 27d * 27d));
        }
        [TestMethod]
        public void M27() {
            Expr s = "25+33-28*6-66";
            Console.WriteLine(s);
            Console.WriteLine(25 + 33 - 28 * 6 - 66);
        }
        [TestMethod]
        public void M26() {
            Console.WriteLine(System.Text.RegularExpressions.Regex.Match('2'.ToString(), "\\d").Success);
            Console.WriteLine(System.Text.RegularExpressions.Regex.Match('5'.ToString(), "\\d").Success);
            Console.WriteLine(System.Text.RegularExpressions.Regex.Match('c'.ToString(), "\\d").Success);
        }
        [TestMethod]
        public void M25() {
            Console.WriteLine(Math.Log(100, 10));
            Console.WriteLine(Math.Log(10, 100));
        }
        [TestMethod]
        public void M24() {
            Console.WriteLine(Math.Pow(5, 2));
            Console.WriteLine(Math.Pow(2, 5));
        }
        [TestMethod]
        public void M22() {
            var k = new double[4, 4] { { 28.5, 33.2, 77, 885 }, { 15.6, 81, 498, 65 }, { 31.88, 15, 0, 13.4231 }, { 82, 99, 754, 12.9 } };
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    Console.Write(k[i, j]);
                    Console.Write(" , ");
                }
                Console.WriteLine();
            }
            Console.WriteLine("k[3,2]={0}", k[3, 2]);
            Console.WriteLine("k[2,3]={0}", k[2, 3]);
        }
        void ShowMatrix(double[,] x) {
            var len0 = x.GetLength(0);
            var len1 = x.GetLength(1);
            //
        }
        [TestMethod]
        public void M23() {
            var k = new double[3, 4] { { 28.5, 33.2, 77, 885 }, { 15.6, 81, 498, 65 }, { 82, 99, 754, 12.9 } };
            var k2 = new double[4, 3] { { 18.5, 0.2, 85 }, { 5.6, 48, 0 }, { 1, 0, 5 }, { 2, 54, 0 } };
            var m = new Matrix(k2);
            var m2 = m.Multiply(new Matrix(k));
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    Console.Write(m2[i, j]);
                    Console.Write(" , ");
                }
                Console.WriteLine();
            }
            Console.WriteLine("--- --- --- ---- --- -- -- -- --- ---- ---- -- -- - --- - - - --- -- - - -- - -  ------");
            var x = k2.Mul(k);
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    Console.Write(x[i, j]);
                    Console.Write(" , ");
                }
                Console.WriteLine();
            }
        }
        [TestMethod]
        public void M211() {
            var k = new double[4, 4] { { 28.5, 33.2, 77, 885 }, { 15.6, 81, 498, 65 }, { 31.88, 15, 0, 0 }, { 82, 99, 754, 12.9 } };
            var k2 = new double[4, 4] { { 18.5, 0.2, 17, 85 }, { 5.6, 81, 48, 0 }, { 1, 10, 0, 5 }, { 2, 9, 54, 0 } };
            var k3 = k.Mul(k2);
            var k4 = k2.Inverse();
            var k5 = k3.Mul(k4);
            Console.WriteLine(k5.Equal(k));
        }
        [TestMethod]
        public void M21() {
            var k = new double[4, 4] { { 28.5, 33.2, 77, 885 }, { 15.6, 81, 498, 65 }, { 31.88, 15, 0, 0 }, { 82, 99, 754, 12.9 } };
            var k2 = new double[4, 4] { { 18.5, 0.2, 17, 85 }, { 5.6, 81, 48, 0 }, { 1, 10, 0, 5 }, { 2, 9, 54, 0 } };
            var m = new Matrix(k);
            var m2 = m.Multiply(new Matrix(k2));
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    Console.Write(m2[i, j]);
                    Console.Write(" , ");
                }
                Console.WriteLine();
            }
            Console.WriteLine("--- --- --- ---- --- -- -- -- --- ---- ---- -- -- - --- - - - --- -- - - -- - -  ------");
            M20();
        }
        [TestMethod]
        public void M20() {
            var k = new double[4, 4] { { 28.5, 33.2, 77, 885 }, { 15.6, 81, 498, 65 }, { 31.88, 15, 0, 0 }, { 82, 99, 754, 12.9 } };
            var k2 = new double[4, 4] { { 18.5, 0.2, 17, 85 }, { 5.6, 81, 48, 0 }, { 1, 10, 0, 5 }, { 2, 9, 54, 0 } };
            var x = k.Mul(k2);
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    Console.Write(x[i, j]);
                    Console.Write(" , ");
                }
                Console.WriteLine();
            }
        }
        [TestMethod]
        public void Levenshtein_Test() {
            var x1 = "14156[werqpo".Levenshtein("1351450-rutp");
            var x2 = "14156[werqpo".Levenshtein("opqertu-rutp");
            var x3 = "14156[werqpo".Levenshtein("135145opiq34");
            var x4 = "14156[werqpo".Levenshtein("13356ojtrp");
            var x5 = "14156[werqpo".Levenshtein("nm;[ry;ler");
            var x6 = "14156[werqpo".Levenshtein("526098wre0;ddfg");
            Console.WriteLine("{0},{1},{2},{3},{4},{5}", x1, x2, x3, x4, x5, x6);
        }
        [TestMethod]
        public void SortTest1() {
            var ran = new Random();
            var len = ran.Next(10, 500);
            var datalist = new List<double>();
            for (int i = 0; i < len; i++) {
                datalist.Add(ran.Next(1000) + ran.NextDouble());
            }
            datalist.ToArray();
            var data1 = new double[datalist.Count];
            var data2 = new double[datalist.Count];
            var data3 = new double[datalist.Count];
            var data4 = new double[datalist.Count];
            var data5 = new double[datalist.Count];
            var data6 = new double[datalist.Count];
            datalist.CopyTo(data1);
            datalist.CopyTo(data2);
            datalist.CopyTo(data3);
            datalist.CopyTo(data4);
            datalist.CopyTo(data5);
            datalist.CopyTo(data6);
            //
            var sw1 = new Stopwatch();
            sw1.Start();
            data1.BubbleSort();
            sw1.Stop();
            //
            var sw2 = new Stopwatch();
            sw2.Start();
            data2.CocktailSort();
            sw2.Stop();
            //
            var sw3 = new Stopwatch();
            sw3.Start();
            data3.InsertSort();
            sw3.Stop();
            //
            var sw4 = new Stopwatch();
            sw4.Start();
            data4.OddEvenSort();
            sw4.Stop();
            //
            var sw5 = new Stopwatch();
            sw5.Start();
            data5.SelectSort();
            sw5.Stop();
            //
            var sw6 = new Stopwatch();
            sw6.Start();
            data6.QuickSort();
            sw6.Stop();
            //
            var dataj = datalist.ToArray();
            Console.WriteLine("原始数据");
            for (int i = 0; i < datalist.Count; i++) {
                Console.Write(dataj[i]);
                Console.Write(" ");
            }
            Console.WriteLine();
            Console.WriteLine("通过BubbleSort排序后的数据及用时");
            Console.WriteLine("用时:{0}", sw1.ElapsedTicks);
            for (int i = 0; i < datalist.Count; i++) {
                Console.Write(data1[i]);
                Console.Write(" ");
            }
            Console.WriteLine();
            Console.WriteLine("通过CocktailSort排序后的数据及用时");
            Console.WriteLine("用时:{0}", sw2.ElapsedTicks);
            for (int i = 0; i < datalist.Count; i++) {
                Console.Write(data2[i]);
                Console.Write(" ");
            }
            Console.WriteLine();
            Console.WriteLine("通过InsertSort排序后的数据及用时");
            Console.WriteLine("用时:{0}", sw3.ElapsedTicks);
            for (int i = 0; i < datalist.Count; i++) {
                Console.Write(data3[i]);
                Console.Write(" ");
            }
            Console.WriteLine();
            Console.WriteLine("通过OddEvenSort排序后的数据及用时");
            Console.WriteLine("用时:{0}", sw4.ElapsedTicks);
            for (int i = 0; i < datalist.Count; i++) {
                Console.Write(data4[i]);
                Console.Write(" ");
            }
            Console.WriteLine();
            Console.WriteLine("通过SelectSort排序后的数据及用时");
            Console.WriteLine("用时:{0}", sw5.ElapsedTicks);
            for (int i = 0; i < datalist.Count; i++) {
                Console.Write(data5[i]);
                Console.Write(" ");
            }
            Console.WriteLine();
            Console.WriteLine("通过QuickSort排序后的数据及用时");
            Console.WriteLine("用时:{0}", sw6.ElapsedTicks);
            for (int i = 0; i < datalist.Count; i++) {
                Console.Write(data6[i]);
                Console.Write(" ");
            }
        }
    }
}
